package com.cakemallproject.controller;

import com.cakemallproject.pojo.Result;
import com.cakemallproject.pojo.mapperObj.Goods;
import com.cakemallproject.pojo.mapperObj.Recommend;
import com.cakemallproject.service.GoodsService;
import com.cakemallproject.service.RecommendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/recommend")
public class RecommendController {
    @Autowired
    private RecommendService recommendService;
    @Autowired
    private GoodsService goodsService;

    @GetMapping
    public Result list() {
        log.info("获取推荐列表");
        List<Recommend> list = recommendService.list();
        return Result.success(list);
    }

    @GetMapping("/goodsList")
    public Result getGoods() {
        log.info("获取推荐的商品列表");
        List<Recommend> list = recommendService.list();
        List<Goods> goodsList = new ArrayList<>();
        for (Recommend recommend : list) {
            goodsList.add(goodsService.getById(recommend.getGid()));
        }
        return Result.success(goodsList);
    }

    @PostMapping("/{ids}")
    public Result insert(@PathVariable List<Integer> ids) {
        log.info("添加推荐：{}", ids);
        recommendService.insert(ids);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("移除推荐：{}", ids);
        recommendService.delete(ids);
        return Result.success();
    }
}
